package logic

import (
	"album-api/internal/svc"
	"album-api/internal/types"
	"context"
	"errors"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"grpc-common/album/types/album"
	"grpc-common/search/types/search"
	"listenboos-common/tools"
	"strconv"
	"time"
)

type CategoryLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoryLogic {
	return &CategoryLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}
func (l *CategoryLogic) GetAllCategoryList() (resp []*types.CategoryChild, err error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	var req *album.RegReq
	res, err := l.svcCtx.AlbumRpc.GetCategoryList(ctx, req)
	if err != nil {
		return nil, errors.New("调用用户信息接口失败")
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	err = copier.Copy(&resp, &res.CategoryChild)
	if err != nil {
		return nil, err
	}
	return
}
func (l *CategoryLogic) FindAttribute(id string) (resp []*types.Attribute, err error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	var req album.GetC1IdToAttributeReq
	req.Id = id
	res, err := l.svcCtx.AlbumRpc.GetCategoryToAttribute(ctx, &req)
	if err != nil {
		return nil, errors.New("调用用户信息接口失败")
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	err = copier.Copy(&resp, &res.GetC1IdToAttributes)
	if err != nil {
		return nil, err
	}
	return
}

func (l *CategoryLogic) SaveAlbumInfo(token string, req types.SaveAlbumInfoReq) (id int64, err error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	userId, err := tools.ParseToken(token, l.svcCtx.Config.JWT.AccessSecret)
	if err != nil {
		return 0, err
	}
	var in album.SaveAlbumInfoReq
	err = copier.Copy(&in, req)
	if err != nil {
		return 0, err
	}
	in.UserId = userId
	res, err := l.svcCtx.AlbumRpc.SaveAlbumInfo(ctx, &in)
	if err != nil {
		return 0, errors.New("调用SaveAlbumInfo接口失败")
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return res.Id, nil
}

func (l *CategoryLogic) FindUserAlbumPage(token string, req types.FindUserAlbumReq) (*types.Records, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	userId, err := tools.ParseToken(token, l.svcCtx.Config.JWT.AccessSecret)
	if err != nil {
		return nil, err
	}
	var in album.FindUserAlbumReq
	err = copier.Copy(&in, &req)
	if err != nil {
		return nil, err
	}
	in.UserId = userId
	resp, err := l.svcCtx.AlbumRpc.FindUserAlbumPage(ctx, &in)
	if err != nil {
		return nil, errors.New("调用SaveAlbumInfo接口失败")
	}
	var records []*types.Record
	err = copier.Copy(&records, &resp.Records)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return &types.Records{Records: records}, nil
}

func (l *CategoryLogic) GetAlbumInfoById(token string, req types.DetailReq) (*types.AlbumInfo, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	userId, err := tools.ParseToken(token, l.svcCtx.Config.JWT.AccessSecret)
	if err != nil {
		return nil, err
	}
	in := album.DetailReq{}
	//err = copier.Copy(&in, &req)
	//if err != nil {
	//	return nil, err
	//}
	albumId, _ := strconv.ParseInt(req.AlbumId, 10, 64)
	in.AlbumId = albumId
	in.UserId = userId
	res, err := l.svcCtx.AlbumRpc.GetAlbumInfoById(ctx, &in)
	if err != nil {
		return nil, err
	}
	var resp types.AlbumInfo
	err = copier.Copy(&resp, res)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return &resp, nil
}

func (l *CategoryLogic) RemoveAlbumInfo(token string, req types.RemoteAlbumReq) error {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	userId, err := tools.ParseToken(token, l.svcCtx.Config.JWT.AccessSecret)
	if err != nil {
		return err
	}
	in := album.RemoteReq{}
	//err = copier.Copy(&in, &req)
	//if err != nil {
	//	return nil, err
	//}
	albumId, _ := strconv.ParseInt(req.AlbumId, 10, 64)
	in.AlbumId = albumId
	in.UserId = userId
	_, err = l.svcCtx.AlbumRpc.RemoteAlbumInfo(ctx, &in)
	if err != nil {
		return errors.New("调用RemoveAlbumInfo接口失败")
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return nil
}

func (l *CategoryLogic) FindUserAllAlbumList(token string) ([]*types.Record, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	userId, err := tools.ParseToken(token, l.svcCtx.Config.JWT.AccessSecret)
	if err != nil {
		return nil, err
	}
	in := album.AlbumListReq{}
	//err = copier.Copy(&in, &req)
	//if err != nil {
	//	return nil, err
	//}
	in.UserId = userId
	res, err := l.svcCtx.AlbumRpc.FindUserAllAlbumList(ctx, &in)
	if err != nil {
		return nil, errors.New("调用SaveAlbumInfo接口失败")
	}
	var resp []*types.Record
	err = copier.Copy(&resp, res.Records)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return resp, nil
}

func (l *CategoryLogic) GetTrackInfoById(token string, req types.TrackInfoReq) (*types.TrackInfoResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	userId, err := tools.ParseToken(token, l.svcCtx.Config.JWT.AccessSecret)
	if err != nil {
		return nil, err
	}
	in := album.TrackInfoReq{}
	//err = copier.Copy(&in, &req)
	//if err != nil {
	//	return nil, err
	//}
	trackId, _ := strconv.ParseInt(req.TrackId, 10, 64)
	in.TrackId = trackId
	in.UserId = userId
	res, err := l.svcCtx.AlbumRpc.GetTrackInfoById(ctx, &in)
	if err != nil {
		return nil, errors.New("调用GetTrackInfoById接口失败")
	}
	var resp types.TrackInfoResp
	err = copier.Copy(&resp, res)
	if err != nil {
		return nil, err
	}
	resp.Id = res.Id
	resp.AlbumId = res.AlbumId
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return &resp, nil
}

func (l *CategoryLogic) GetCategory3ListByCategory1Id(req types.Category1Req) (c3s []*types.Category3, err error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.GetCategory3ListByCategory1IdReq{}
	c1Id, _ := strconv.ParseInt(req.Category1Id, 10, 64)
	in.Category1Id = c1Id
	res, err := l.svcCtx.AlbumRpc.GetCategory3ListByCategory1Id(ctx, &in)
	if err != nil {
		return nil, errors.New("调用GetCategory3ListByCategory1Id接口失败")
	}
	err = copier.Copy(&c3s, res.CategoryInfo)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return c3s, nil
}

func (l *CategoryLogic) GetChannel(req types.Category1Req) ([]*types.ChannelResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.CategoryChannelReq{}
	c1Id, _ := strconv.ParseInt(req.Category1Id, 10, 64)
	in.Category1Id = c1Id
	res, err := l.svcCtx.AlbumRpc.GetCategoryChannel(ctx, &in)
	if err != nil {
		return nil, errors.New("调用GetCategory3ListByCategory1Id接口失败")
	}
	var resp []*types.ChannelResp
	err = copier.Copy(&resp, &res.Category)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return resp, nil
}

func (l *CategoryLogic) GetBaseCategoryList3(req types.Category1Req) (*types.GetBaseCategoryList3Resp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.GetBaseCategoryList3Req{}
	c1Id, _ := strconv.ParseInt(req.Category1Id, 10, 64)
	in.Category1Id = c1Id
	res, err := l.svcCtx.AlbumRpc.GetBaseCategoryList3(ctx, &in)
	if err != nil {
		return nil, errors.New("调用GetCategory3ListByCategory1Id接口失败")
	}
	var cate types.GetBaseCategoryList3Resp
	err = copier.Copy(&cate, &res)
	if err != nil {
		return nil, err
	}
	return &cate, nil
}

func (l *CategoryLogic) SearchAlbumInfo(req types.AlbumInfoReq) (*types.AlbumInfoResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.AlbumInfoReq{}
	err := copier.Copy(&in, &req)
	if err != nil {
		return nil, err
	}
	res, err := l.svcCtx.AlbumRpc.SearchAlbumInfo(ctx, &in)
	if err != nil {
		return nil, errors.New("调用GetCategory3ListByCategory1Id接口失败")
	}
	albums := types.AlbumInfoResp{}
	err = copier.Copy(&albums, &res)
	if err != nil {
		return nil, err
	}
	return &albums, nil
}

// TODO:未完成，search服务层未写完，有空记得补充
func (l *CategoryLogic) KeyWord(req types.KeyWordReq) ([]string, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.KeywordReq{}
	in.Keyword = req.Keyword
	res, err := l.svcCtx.AlbumRpc.KeyWord(ctx, &in)
	if err != nil {
		return nil, errors.New("调用KeyWord接口失败")
	}

	return res.Keywords, nil
}

func (l *CategoryLogic) FindAlbumInfo(req types.FindAlbumInfoReq) (*types.FindAlbumResp, error) { //*types.FindAlbumResp
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.FindAlbumInfoReq{}
	in.AlbumId = req.AlbumId
	res, err := l.svcCtx.AlbumRpc.FindAlbumInfo(ctx, &in)
	if err != nil {
		return nil, errors.New("调用FindAlbumInfo接口失败")
	}
	albumInfo := types.AlbumInfoV1{}
	err = copier.Copy(&albumInfo, &res.AlbumInfo)
	bresp := types.BaseCategoryView{}
	err = copier.Copy(&bresp, &res.BaseCategoryView)
	userInfo := types.Announcer{}
	err = copier.Copy(&userInfo, &res.Announcer)
	statResp := types.AlbumStatVo{}
	err = copier.Copy(&statResp, &res.AlbumStatVo)
	if err != nil {
		return nil, err
	}
	//if statResp.SubscribeStatNum == 0 {
	//	statResp.SubscribeStatNum = 1
	//	statResp.BuyStatNum = 1
	//	statResp.CommentStatNum = 1
	//	statResp.PlayStatNum = 1
	//}
	//response := make(map[string]interface{})
	//response["albumInfo"] = albumInfo
	//response["announcer"] = userInfo
	//response["baseCategoryView"] = bresp
	//response["albumStatVo"] = statResp

	return &types.FindAlbumResp{Album: &albumInfo, BaseCategoryView: &bresp, Announcer: &userInfo, AlbumStatVo: &statResp}, nil
	//return response, nil
}

func (l *CategoryLogic) FindAlbumTrackPage(req types.FindTrackReq, userId int64) (*types.FindTrackResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.FindTrackReq{}
	in.PageNo = req.PageNo
	in.PageSize = req.PageSize
	albumId, _ := strconv.ParseInt(req.AlbumId, 10, 64)
	in.AlbumId = albumId
	in.UserId = userId
	rres, err := l.svcCtx.AlbumRpc.FindAlbumTrackPage(ctx, &in)
	if err != nil {
		return nil, err
	}
	var resp types.FindTrackResp
	if err != nil {
		return nil, errors.New("rpc调用FindAlbumTrackPage接口失败")
	}
	err = copier.Copy(&resp, rres)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return &resp, nil
}

func (l *CategoryLogic) FindRankingList(req types.FindRankingListReq) ([]*types.AlbumInfoV1, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := search.UpdateRankingReq{}
	in.Dimension = req.Dimension
	in.Category1Id = req.Category1Id
	rres, err := l.svcCtx.SearchRpc.UpdateRanking(ctx, &in)
	if err != nil {
		return nil, err
	}
	var resp []*types.AlbumInfoV1
	if err != nil {
		return nil, errors.New("rpc调用FindAlbumTrackPage接口失败")
	}
	err = copier.Copy(&resp, &rres.AlbumList)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return resp, nil
}
func (l *CategoryLogic) UpdateListenProcess(req types.UpdateListenReq, userId int64) error {
	ctx, cancel := context.WithTimeout(context.Background(), 50*time.Minute)
	defer cancel()
	// 在这里调用微信接口还是其他地方？
	//albumId, _ := strconv.ParseInt(req.AlbumId, 10, 64)
	re := &album.UpdateListenReq{}
	re.AlbumId = req.AlbumId
	re.TrackId = req.TrackId
	re.BreakSecond = float32(req.BreakSecond)
	re.UserId = userId
	_, err := l.svcCtx.AlbumRpc.UpdateListenProcess(ctx, re)
	if err != nil {
		return errors.New("调用UpdateListenProcess接口失败")
	}
	// 在这里调用微信接口还是其他地方？
	//resp := []*types.UpdateListenResp{}
	//err = copier.Copy(&resp, &vips.VipServiceConfig)
	return nil
}

func (l *CategoryLogic) GetLatelyTack(userId int64) (*types.GetLatelyTackResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.GetLatelyTackReq{}
	in.UserId = userId
	rres, err := l.svcCtx.AlbumRpc.GetLatelyTack(ctx, &in)
	if err != nil {
		return nil, err
	}
	resp := &types.GetLatelyTackResp{}
	if err != nil {
		return nil, errors.New("rpc调用GetLatelyTack接口失败")
	}
	err = copier.Copy(&resp, &rres)
	if err != nil {
		return nil, err
	}
	// method GetCategoryList not implemented
	//var resp []*types.CategoryChild
	return resp, nil
}

func (l *CategoryLogic) GetTrackStatVo(req types.GetTrackStatVoReq) (*types.GetTrackStatVoResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.GetTrackStatVoReq{}
	in.TrackId = req.TrackId
	res, err := l.svcCtx.AlbumRpc.GetTrackStatVo(ctx, &in)
	if err != nil {
		return nil, errors.New("调用FindAlbumInfo接口失败")
	}
	trackVo := types.GetTrackStatVoResp{}
	err = copier.Copy(&trackVo, &res)
	if err != nil {
		return nil, err
	}
	return &trackVo, nil
}

func (l *CategoryLogic) FindUserTrackPaidList(req types.GetTrackStatVoReq, id int64) ([]*types.GetUserPaidTrackList, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.FindUserTrackPaidListReq{}
	in.TrackId = req.TrackId
	in.UserId = id
	res, err := l.svcCtx.AlbumRpc.FindUserTrackPaidList(ctx, &in)
	if err != nil {
		return nil, errors.New("调用FindUserTrackPaidList接口失败")
	}
	var trackVo []*types.GetUserPaidTrackList
	err = copier.Copy(&trackVo, &res.TrackSettingInterface)
	if err != nil {
		return nil, err
	}
	return trackVo, nil
}

func (l *CategoryLogic) OrderInfo(req types.OrderInfoReq, userId int64) (*types.OrderInfoResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.GetSubmitOrderInfoReq{}
	err := copier.Copy(&in, &req)
	if err != nil {
		return nil, err
	}
	in.UserId = userId
	res, err := l.svcCtx.AlbumRpc.GetSubmitOrderInfo(ctx, &in)
	if err != nil {
		return nil, errors.New("调用FindUserTrackPaidList接口失败")
	}
	trackVo := types.OrderInfoResp{}
	err = copier.Copy(&trackVo, &res)
	if err != nil {
		return nil, err
	}
	return &trackVo, nil
}

func (l *CategoryLogic) SubmitOrder(req types.SubmitOrderReq, userId int64) (*types.SubmitOrderResp, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
	defer cancel()
	in := album.SubmitOrderReq{}
	err := copier.Copy(&in, &req)
	if err != nil {
		return nil, err
	}
	in.UserId = userId
	res, err := l.svcCtx.AlbumRpc.SubmitOrder(ctx, &in)
	if err != nil {
		return nil, errors.New("调用FindUserTrackPaidList接口失败")
	}
	trackVo := types.SubmitOrderResp{}
	err = copier.Copy(&trackVo, &res)
	if err != nil {
		return nil, err
	}
	return &trackVo, nil
}
